<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Date 日期时间 | MyUI 4.x</title>
    <meta name="generator" content="VuePress 1.5.4">
    <link rel="icon" href="/my/favicon.ico">
    <script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?c4e5f73318b5cb0c389e3d9a05f831cc";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
    <meta name="description" content="MyUI是美亚柏科旗下新德汇出品的Web前端一站式项目工程框架，专注于公检法行业中台系统快速搭建，框架已在多个项目实战检验。">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <link rel="preload" href="/my/assets/css/0.styles.810d4cba.css" as="style"><link rel="preload" href="/my/assets/js/app.2dbd0b81.js" as="script"><link rel="preload" href="/my/assets/js/493.009afff8.js" as="script"><link rel="preload" href="/my/assets/js/1388.8e4e54ce.js" as="script"><link rel="preload" href="/my/assets/js/1246.59f69ef0.js" as="script"><link rel="preload" href="/my/assets/js/1031.8d6cd2c9.js" as="script">
    <link rel="stylesheet" href="/my/assets/css/0.styles.810d4cba.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/my/" class="home-link router-link-active"><img src="/my/img/logo.png" alt="MyUI 4.x" class="logo"> <span class="site-name can-hide">MyUI 4.x</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/my/guide/" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="/my/ui/components/" class="nav-link router-link-active">
  组件
</a></div><div class="nav-item"><a href="/my/ui/charts/" class="nav-link">
  图表
</a></div><div class="nav-item"><a href="/my/ui/map/" class="nav-link">
  地图
</a></div><div class="nav-item"><a href="/my/ui/go/" class="nav-link">
  关系图
</a></div><div class="nav-item"><a href="/my/ui/icon/" class="nav-link">
  图标
</a></div><div class="nav-item"><a href="/my/ui/pages/" class="nav-link">
  页面
</a></div><div class="nav-item"><a href="http://newgateway.gitee.io/my-web/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  演示
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="JSDoc" class="dropdown-title"><span class="title">JSDoc</span> <span class="arrow down"></span></button> <button type="button" aria-label="JSDoc" class="mobile-dropdown-title"><span class="title">JSDoc</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/my/api/components.html" class="nav-link">
  基础组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/utils.html" class="nav-link">
  工具函数
</a></li><li class="dropdown-item"><!----> <a href="/my/api/charts.html" class="nav-link">
  图表组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/map.html" class="nav-link">
  地图组件
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/my/guide/" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="/my/ui/components/" class="nav-link router-link-active">
  组件
</a></div><div class="nav-item"><a href="/my/ui/charts/" class="nav-link">
  图表
</a></div><div class="nav-item"><a href="/my/ui/map/" class="nav-link">
  地图
</a></div><div class="nav-item"><a href="/my/ui/go/" class="nav-link">
  关系图
</a></div><div class="nav-item"><a href="/my/ui/icon/" class="nav-link">
  图标
</a></div><div class="nav-item"><a href="/my/ui/pages/" class="nav-link">
  页面
</a></div><div class="nav-item"><a href="http://newgateway.gitee.io/my-web/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  演示
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="JSDoc" class="dropdown-title"><span class="title">JSDoc</span> <span class="arrow down"></span></button> <button type="button" aria-label="JSDoc" class="mobile-dropdown-title"><span class="title">JSDoc</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/my/api/components.html" class="nav-link">
  基础组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/utils.html" class="nav-link">
  工具函数
</a></li><li class="dropdown-item"><!----> <a href="/my/api/charts.html" class="nav-link">
  图表组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/map.html" class="nav-link">
  地图组件
</a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>功能辅助</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>基础元件</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/my/ui/components/corner-mark.html" class="sidebar-link">CornerMark 角标</a></li><li><a href="/my/ui/components/date.html" aria-current="page" class="active sidebar-link">Date 日期时间</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#何时使用" class="sidebar-link">何时使用</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#代码示例" class="sidebar-link">代码示例</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#基础用法" class="sidebar-link">基础用法</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#自定义格式" class="sidebar-link">自定义格式</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#相对时间" class="sidebar-link">相对时间</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#文本颜色" class="sidebar-link">文本颜色</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#心跳" class="sidebar-link">心跳</a></li></ul></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#dayjs-api" class="sidebar-link">Dayjs API</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#解析" class="sidebar-link">解析</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#时间字符串" class="sidebar-link">时间字符串</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#date-对象" class="sidebar-link">Date 对象</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#unix-时间戳-毫秒" class="sidebar-link">Unix 时间戳 (毫秒)</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#unix-时间戳-秒" class="sidebar-link">Unix 时间戳 (秒)</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#自定义时间格式" class="sidebar-link">自定义时间格式</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#复制" class="sidebar-link">复制</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#验证" class="sidebar-link">验证</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#获取-设置" class="sidebar-link">获取+设置</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#操作" class="sidebar-link">操作</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#显示" class="sidebar-link">显示</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#utc-偏移量-分" class="sidebar-link">UTC 偏移量 (分)</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#查询" class="sidebar-link">查询</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/date.html#是否是-dayjs-isdayjs-compared-any" class="sidebar-link">是否是 Dayjs .isDayjs(compared: any)</a></li></ul></li></ul></li><li><a href="/my/ui/components/empty.html" class="sidebar-link">Empty 空状态</a></li><li><a href="/my/ui/components/header.html" class="sidebar-link">Header 头部</a></li><li><a href="/my/ui/components/icon.html" class="sidebar-link">Icon 图标</a></li><li><a href="/my/ui/components/number.html" class="sidebar-link">Number 数字</a></li><li><a href="/my/ui/components/result.html" class="sidebar-link">Result 结果</a></li><li><a href="/my/ui/components/skeleton.html" class="sidebar-link">Skeleton 骨架屏</a></li><li><a href="/my/ui/components/spin.html" class="sidebar-link">Spin 加载中</a></li><li><a href="/my/ui/components/timer.html" class="sidebar-link">Timer 计时器</a></li><li><a href="/my/ui/components/typography.html" class="sidebar-link">Typography 文本排版</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>排版布局</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>容器修饰</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>导航菜单</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据录入</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据展示</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>动画效果</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="date-日期时间"><a href="#date-日期时间" class="header-anchor">#</a> Date 日期时间</h1> <p>基于 Dayjs 类库的日期时间格式化组件。
组件文档 <a href="/my/api/components.html?url=%2Fmy%2Fapi%2Fcomponents%2Fmodule-%24ui_components_my-date.html" target="_blank" data-v-3de95506>MyDate <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound" data-v-3de95506 data-v-3de95506><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <h2 id="何时使用"><a href="#何时使用" class="header-anchor">#</a> 何时使用</h2> <p>需要对日期时间格式化展示时</p> <h2 id="代码示例"><a href="#代码示例" class="header-anchor">#</a> 代码示例</h2> <h3 id="基础用法"><a href="#基础用法" class="header-anchor">#</a> 基础用法</h3> <p>显示当前时间</p> <!----><h3 id="自定义格式"><a href="#自定义格式" class="header-anchor">#</a> 自定义格式</h3> <p>详情如下:</p> <table><thead><tr><th>格式</th> <th>输出</th> <th>描述</th></tr></thead> <tbody><tr><td><code>YY</code></td> <td>18</td> <td>两位数的年份</td></tr> <tr><td><code>YYYY</code></td> <td>2018</td> <td>四位数的年份</td></tr> <tr><td><code>M</code></td> <td>1-12</td> <td>月份，从 1 开始</td></tr> <tr><td><code>MM</code></td> <td>01-12</td> <td>月份，两位数</td></tr> <tr><td><code>MMM</code></td> <td>Jan-Dec</td> <td>简写的月份名称</td></tr> <tr><td><code>MMMM</code></td> <td>January-December</td> <td>完整的月份名称</td></tr> <tr><td><code>D</code></td> <td>1-31</td> <td>月份里的一天</td></tr> <tr><td><code>DD</code></td> <td>01-31</td> <td>月份里的一天，两位数</td></tr> <tr><td><code>d</code></td> <td>0-6</td> <td>一周中的一天，星期天是 0</td></tr> <tr><td><code>dd</code></td> <td>Su-Sa</td> <td>最简写的一周中一天的名称</td></tr> <tr><td><code>ddd</code></td> <td>Sun-Sat</td> <td>简写的一周中一天的名称</td></tr> <tr><td><code>dddd</code></td> <td>Sunday-Saturday</td> <td>一周中一天的名称</td></tr> <tr><td><code>H</code></td> <td>0-23</td> <td>小时</td></tr> <tr><td><code>HH</code></td> <td>00-23</td> <td>小时，两位数</td></tr> <tr><td><code>h</code></td> <td>1-12</td> <td>小时, 12 小时制</td></tr> <tr><td><code>hh</code></td> <td>01-12</td> <td>Hours, 12 小时制, 两位数</td></tr> <tr><td><code>m</code></td> <td>0-59</td> <td>分钟</td></tr> <tr><td><code>mm</code></td> <td>00-59</td> <td>分钟，两位数</td></tr> <tr><td><code>s</code></td> <td>0-59</td> <td>秒</td></tr> <tr><td><code>ss</code></td> <td>00-59</td> <td>秒 两位数</td></tr> <tr><td><code>SSS</code></td> <td>000-999</td> <td>毫秒 三位数</td></tr> <tr><td><code>Z</code></td> <td>+5:00</td> <td>UTC 的偏移量</td></tr> <tr><td><code>ZZ</code></td> <td>+0500</td> <td>UTC 的偏移量，数字前面加上 0</td></tr> <tr><td><code>A</code></td> <td>AM PM</td> <td></td></tr> <tr><td><code>a</code></td> <td>am pm</td> <td></td></tr></tbody></table> <!----><h3 id="相对时间"><a href="#相对时间" class="header-anchor">#</a> 相对时间</h3> <table><thead><tr><th>Range</th> <th>Key</th> <th>Sample Output</th></tr></thead> <tbody><tr><td>0 到 44 秒</td> <td>s</td> <td>几秒前</td></tr> <tr><td>45 到 89 秒</td> <td>m</td> <td>1 分钟前</td></tr> <tr><td>90 秒 到 44 分</td> <td>mm</td> <td>2 分钟前 ... 44 分钟前</td></tr> <tr><td>45 到 89 分</td> <td>h</td> <td>1 小时前</td></tr> <tr><td>90 分 到 21 小时</td> <td>hh</td> <td>2 小时前 ... 21 小时前</td></tr> <tr><td>22 到 35 小时</td> <td>d</td> <td>1 天前</td></tr> <tr><td>36 小时 到 25 天</td> <td>dd</td> <td>2 天前 ... 25 天前</td></tr> <tr><td>26 到 45 天</td> <td>M</td> <td>1 个月前</td></tr> <tr><td>46 天 到 10 月</td> <td>MM</td> <td>2 个月前 ... 10 个月前</td></tr> <tr><td>11 月 到 17 月</td> <td>y</td> <td>1 年前</td></tr> <tr><td>18 月以上</td> <td>yy</td> <td>2 年前 ... 20 年前</td></tr></tbody></table> <!----><h3 id="文本颜色"><a href="#文本颜色" class="header-anchor">#</a> 文本颜色</h3> <!----><h3 id="心跳"><a href="#心跳" class="header-anchor">#</a> 心跳</h3> <!----><h2 id="dayjs-api"><a href="#dayjs-api" class="header-anchor">#</a> Dayjs API</h2> <p><code>Dayjs</code> 并没有改变或覆盖 Javascript 原生的 <code>Date.prototype</code>， 而是创造了一个全新的包含 <code>Javascript Date</code> 对象的 <code>Dayjs</code> 的对象。</p> <p><code>Dayjs</code> 对象是不可变的, 所有的 API 操作都将返回一个新的 <code>Dayjs</code> 对象。</p> <p>如果没有特别说明，Day.js 的返回值都是新的 <code>Dayjs</code> 对象。</p> <h3 id="解析"><a href="#解析" class="header-anchor">#</a> 解析</h3> <p>在 <code>dayjs()</code> 中传入支持的格式</p> <h4 id="当前时间"><a href="#当前时间" class="header-anchor">#</a> 当前时间</h4> <p>直接运行 <code>dayjs()</code>，得到包含当前时间和日期的 <code>Dayjs</code> 对象。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="时间字符串"><a href="#时间字符串" class="header-anchor">#</a> 时间字符串</h3> <p>可以解析传入的一个标准的<a href="https://en.wikipedia.org/wiki/ISO_8601" target="_blank" rel="noopener noreferrer">ISO 8601<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>时间字符串。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span>String<span class="token punctuation">)</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token string">'1995-12-25'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="date-对象"><a href="#date-对象" class="header-anchor">#</a> Date 对象</h3> <p>可以解析传入的一个 Javascript Date 对象。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span>Date<span class="token punctuation">)</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2018</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="unix-时间戳-毫秒"><a href="#unix-时间戳-毫秒" class="header-anchor">#</a> Unix 时间戳 (毫秒)</h3> <p>可以解析传入的一个 Unix 时间戳 (13 位数字)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span>Number<span class="token punctuation">)</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token number">1318781876406</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="unix-时间戳-秒"><a href="#unix-时间戳-秒" class="header-anchor">#</a> Unix 时间戳 (秒)</h3> <p>可以解析传入的一个 Unix 时间戳 (10 位数字)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>dayjs<span class="token punctuation">.</span><span class="token function">unix</span><span class="token punctuation">(</span>Number<span class="token punctuation">)</span>
dayjs<span class="token punctuation">.</span><span class="token function">unix</span><span class="token punctuation">(</span><span class="token number">1318781876</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="自定义时间格式"><a href="#自定义时间格式" class="header-anchor">#</a> 自定义时间格式</h3> <ul><li>解析自定义时间格式如 <code>dayjs(&quot;12-25-1995&quot;, &quot;MM-DD-YYYY&quot;)</code> 可以使用插件 <code>CustomParseFormat</code></li></ul> <h3 id="复制"><a href="#复制" class="header-anchor">#</a> 复制</h3> <p><code>Dayjs</code> 对象是不可变的，如果您想获得一个对象的拷贝，请执行 <code>.clone()</code>。
向 <code>dayjs()</code> 里传入一个 <code>Dayjs</code> 对象也能实现同样的效果。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span>Dayjs<span class="token punctuation">)</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="验证"><a href="#验证" class="header-anchor">#</a> 验证</h3> <ul><li>return Boolean</li></ul> <p>检测当前 <code>Dayjs</code> 对象是否是一个有效的时间。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">isValid</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><hr> <h3 id="获取-设置"><a href="#获取-设置" class="header-anchor">#</a> 获取+设置</h3> <p>获取和改变日期。</p> <h4 id="年"><a href="#年" class="header-anchor">#</a> 年</h4> <p>获取或设置年份。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">year</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">year</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="月"><a href="#月" class="header-anchor">#</a> 月</h4> <p>获取或设置月份。从 0 开始</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">month</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">month</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="日"><a href="#日" class="header-anchor">#</a> 日</h4> <p>获取或设置日期。从 1 开始</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">date</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">date</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="星期"><a href="#星期" class="header-anchor">#</a> 星期</h4> <p>获取或设置星期。从星期天 0 开始</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">day</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">day</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="时"><a href="#时" class="header-anchor">#</a> 时</h4> <p>获取或设置小时。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">hour</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">hour</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="分"><a href="#分" class="header-anchor">#</a> 分</h4> <p>获取或设置分钟。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">minute</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">minute</span><span class="token punctuation">(</span><span class="token number">59</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="秒"><a href="#秒" class="header-anchor">#</a> 秒</h4> <p>获取或设置秒。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">second</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">second</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="毫秒"><a href="#毫秒" class="header-anchor">#</a> 毫秒</h4> <p>获取或设置毫秒。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">millisecond</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">millisecond</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="获取"><a href="#获取" class="header-anchor">#</a> 获取</h4> <p>获取从 <code>Dayjs</code> 对象中取到的信息
传入的单位 (unit) 对大小写不敏感。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>unit <span class="token operator">:</span> String<span class="token punctuation">)</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'month'</span><span class="token punctuation">)</span> <span class="token comment">// 从 0 开始</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'day'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="可用单位"><a href="#可用单位" class="header-anchor">#</a> 可用单位</h4> <table><thead><tr><th>单位</th> <th>缩写</th> <th>描述</th></tr></thead> <tbody><tr><td><code>date</code></td> <td></td> <td>日期</td></tr> <tr><td><code>day</code></td> <td><code>d</code></td> <td>星期几 (星期天 0, 星期六 6)</td></tr> <tr><td><code>month</code></td> <td><code>M</code></td> <td>月 (一月 0, 十二月 11)</td></tr> <tr><td><code>year</code></td> <td><code>y</code></td> <td>年</td></tr> <tr><td><code>hour</code></td> <td><code>h</code></td> <td>时</td></tr> <tr><td><code>minute</code></td> <td><code>m</code></td> <td>分</td></tr> <tr><td><code>second</code></td> <td><code>s</code></td> <td>秒</td></tr> <tr><td><code>millisecond</code></td> <td><code>ms</code></td> <td>毫秒</td></tr></tbody></table> <h4 id="设置"><a href="#设置" class="header-anchor">#</a> 设置</h4> <p>设置时间</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>unit <span class="token operator">:</span> String<span class="token punctuation">,</span> value <span class="token operator">:</span> Int<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">'date'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">'month'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 四月</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">'second'</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="操作"><a href="#操作" class="header-anchor">#</a> 操作</h3> <p>您可以对 <code>Dayjs</code> 对象如下增加减少之类的操作：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">startOf</span><span class="token punctuation">(</span><span class="token string">'month'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'day'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">subtract</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'year'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h4 id="增加"><a href="#增加" class="header-anchor">#</a> 增加</h4> <p>增加时间并返回一个新的 <code>Dayjs()</code> 对象。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>value <span class="token operator">:</span> Number<span class="token punctuation">,</span> unit <span class="token operator">:</span> String<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token string">'day'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="减少"><a href="#减少" class="header-anchor">#</a> 减少</h4> <p>减少时间并返回一个新的 <code>Dayjs()</code> 对象。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">subtract</span><span class="token punctuation">(</span>value <span class="token operator">:</span> Number<span class="token punctuation">,</span> unit <span class="token operator">:</span> String<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">subtract</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token string">'year'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="开头时间"><a href="#开头时间" class="header-anchor">#</a> 开头时间</h4> <p>返回当前时间的开头时间的 <code>Dayjs()</code> 对象，如月份的第一天。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">startOf</span><span class="token punctuation">(</span>unit <span class="token operator">:</span> String<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">startOf</span><span class="token punctuation">(</span><span class="token string">'week'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 取决于 locale 文件里 `weekStart` 的值</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="末尾时间"><a href="#末尾时间" class="header-anchor">#</a> 末尾时间</h4> <p>返回当前时间的末尾时间的 <code>Dayjs()</code> 对象，如月份的最后一天。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">endOf</span><span class="token punctuation">(</span>unit <span class="token operator">:</span> String<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">endOf</span><span class="token punctuation">(</span><span class="token string">'month'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><hr> <h3 id="显示"><a href="#显示" class="header-anchor">#</a> 显示</h3> <p>格式化 <code>Dayjs</code> 对象并展示。</p> <h4 id="格式化"><a href="#格式化" class="header-anchor">#</a> 格式化</h4> <ul><li>return String</li></ul> <p>接收一系列的时间日期字符串并替换成相应的值。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>String<span class="token punctuation">)</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token string">'2019-01-25'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token string">'[YYYY] YYYY-MM-DDTHH:mm:ssZ[Z]'</span><span class="token punctuation">)</span> <span class="token comment">// 'YYYY 2019-01-25T00:00:00-02:00Z'</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token string">'{YYYY} MM-DDTHH:mm:ssZ[Z]'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;{2014} 09-08T08:02:17-05:00Z&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>详情如下:</p> <table><thead><tr><th>格式</th> <th>输出</th> <th>描述</th></tr></thead> <tbody><tr><td><code>YY</code></td> <td>18</td> <td>两位数的年份</td></tr> <tr><td><code>YYYY</code></td> <td>2018</td> <td>四位数的年份</td></tr> <tr><td><code>M</code></td> <td>1-12</td> <td>月份，从 1 开始</td></tr> <tr><td><code>MM</code></td> <td>01-12</td> <td>月份，两位数</td></tr> <tr><td><code>MMM</code></td> <td>Jan-Dec</td> <td>简写的月份名称</td></tr> <tr><td><code>MMMM</code></td> <td>January-December</td> <td>完整的月份名称</td></tr> <tr><td><code>D</code></td> <td>1-31</td> <td>月份里的一天</td></tr> <tr><td><code>DD</code></td> <td>01-31</td> <td>月份里的一天，两位数</td></tr> <tr><td><code>d</code></td> <td>0-6</td> <td>一周中的一天，星期天是 0</td></tr> <tr><td><code>dd</code></td> <td>Su-Sa</td> <td>最简写的一周中一天的名称</td></tr> <tr><td><code>ddd</code></td> <td>Sun-Sat</td> <td>简写的一周中一天的名称</td></tr> <tr><td><code>dddd</code></td> <td>Sunday-Saturday</td> <td>一周中一天的名称</td></tr> <tr><td><code>H</code></td> <td>0-23</td> <td>小时</td></tr> <tr><td><code>HH</code></td> <td>00-23</td> <td>小时，两位数</td></tr> <tr><td><code>h</code></td> <td>1-12</td> <td>小时, 12 小时制</td></tr> <tr><td><code>hh</code></td> <td>01-12</td> <td>Hours, 12 小时制, 两位数</td></tr> <tr><td><code>m</code></td> <td>0-59</td> <td>分钟</td></tr> <tr><td><code>mm</code></td> <td>00-59</td> <td>分钟，两位数</td></tr> <tr><td><code>s</code></td> <td>0-59</td> <td>秒</td></tr> <tr><td><code>ss</code></td> <td>00-59</td> <td>秒 两位数</td></tr> <tr><td><code>SSS</code></td> <td>000-999</td> <td>毫秒 三位数</td></tr> <tr><td><code>Z</code></td> <td>+5:00</td> <td>UTC 的偏移量</td></tr> <tr><td><code>ZZ</code></td> <td>+0500</td> <td>UTC 的偏移量，数字前面加上 0</td></tr> <tr><td><code>A</code></td> <td>AM PM</td> <td></td></tr> <tr><td><code>a</code></td> <td>am pm</td> <td></td></tr></tbody></table> <ul><li>更多格式化的选项 <code>Q Do k kk X x ...</code> 可以使用插件 <code>AdvancedFormat</code></li> <li>本地化的长日期格式 <code>L LT LTS ...</code> 可以使用插件 <code>LocalizedFormat</code></li></ul> <h4 id="时间差"><a href="#时间差" class="header-anchor">#</a> 时间差</h4> <ul><li>return Number</li></ul> <p>获取两个 <code>Dayjs</code> 对象的时间差，默认毫秒。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> date1 <span class="token operator">=</span> <span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token string">'2019-01-25'</span><span class="token punctuation">)</span>
<span class="token keyword">const</span> date2 <span class="token operator">=</span> <span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token string">'2018-06-05'</span><span class="token punctuation">)</span>
date1<span class="token punctuation">.</span><span class="token function">diff</span><span class="token punctuation">(</span>date2<span class="token punctuation">)</span> <span class="token comment">// 20214000000 </span>
date1<span class="token punctuation">.</span><span class="token function">diff</span><span class="token punctuation">(</span>date2<span class="token punctuation">,</span> <span class="token string">'month'</span><span class="token punctuation">)</span> <span class="token comment">// 7</span>
date1<span class="token punctuation">.</span><span class="token function">diff</span><span class="token punctuation">(</span>date2<span class="token punctuation">,</span> <span class="token string">'month'</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token comment">// 7.645161290322581</span>
date1<span class="token punctuation">.</span><span class="token function">diff</span><span class="token punctuation">(</span>date2<span class="token punctuation">,</span> <span class="token string">'day'</span><span class="token punctuation">)</span> <span class="token comment">// 233</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h4 id="unix-时间戳-毫秒-2"><a href="#unix-时间戳-毫秒-2" class="header-anchor">#</a> Unix 时间戳 (毫秒)</h4> <ul><li>return Number</li></ul> <p>返回 Unix 时间戳 (毫秒)</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="unix-时间戳-秒-2"><a href="#unix-时间戳-秒-2" class="header-anchor">#</a> Unix 时间戳 (秒)</h4> <ul><li>return Number</li></ul> <p>返回 Unix 时间戳 (秒)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unix</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="utc-偏移量-分"><a href="#utc-偏移量-分" class="header-anchor">#</a> UTC 偏移量 (分)</h3> <p>返回 UTC 偏移量 (分)</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">utcOffset</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="天数-月"><a href="#天数-月" class="header-anchor">#</a> 天数 (月)</h4> <ul><li>return Number</li></ul> <p>返回月份的天数。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">daysInMonth</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="date-对象-2"><a href="#date-对象-2" class="header-anchor">#</a> Date 对象</h4> <ul><li>return Javascript <code>Date</code> object</li></ul> <p>返回原生的 <code>Date</code> 对象。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="as-json"><a href="#as-json" class="header-anchor">#</a> As JSON</h4> <ul><li>return JSON String</li></ul> <p>当序列化 <code>Dayjs</code> 对象时，会返回 ISO8601 格式的字符串。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toJSON</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//&quot;2018-08-08T00:00:00.000Z&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="iso-8601-字符串"><a href="#iso-8601-字符串" class="header-anchor">#</a> ISO 8601 字符串</h4> <ul><li>return String</li></ul> <p>返回 ISO8601 格式的字符串。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toISOString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="字符串"><a href="#字符串" class="header-anchor">#</a> 字符串</h4> <ul><li>return String</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><hr> <h3 id="查询"><a href="#查询" class="header-anchor">#</a> 查询</h3> <h4 id="是否之前"><a href="#是否之前" class="header-anchor">#</a> 是否之前</h4> <ul><li>return Boolean</li></ul> <p>检查一个 <code>Dayjs</code> 对象是否在另一个 <code>Dayjs</code> 对象时间之前。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">isBefore</span><span class="token punctuation">(</span>Dayjs<span class="token punctuation">,</span> unit<span class="token operator">?</span> <span class="token operator">:</span> String<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">isBefore</span><span class="token punctuation">(</span><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">isBefore</span><span class="token punctuation">(</span><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'year'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="是否相同"><a href="#是否相同" class="header-anchor">#</a> 是否相同</h4> <ul><li>return Boolean</li></ul> <p>检查一个 <code>Dayjs</code> 对象是否和另一个 <code>Dayjs</code> 对象时间相同。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">isSame</span><span class="token punctuation">(</span>Dayjs<span class="token punctuation">,</span> unit<span class="token operator">?</span> <span class="token operator">:</span> String<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">isSame</span><span class="token punctuation">(</span><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">isSame</span><span class="token punctuation">(</span><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'year'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="是否之后"><a href="#是否之后" class="header-anchor">#</a> 是否之后</h4> <ul><li>return Boolean</li></ul> <p>检查一个 <code>Dayjs</code> 对象是否在另一个 <code>Dayjs</code> 对象时间之后。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">isAfter</span><span class="token punctuation">(</span>Dayjs<span class="token punctuation">,</span> unit<span class="token operator">?</span> <span class="token operator">:</span> String<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">isAfter</span><span class="token punctuation">(</span><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
<span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">isAfter</span><span class="token punctuation">(</span><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'year'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="是否是-dayjs-isdayjs-compared-any"><a href="#是否是-dayjs-isdayjs-compared-any" class="header-anchor">#</a> 是否是 Dayjs <code>.isDayjs(compared: any)</code></h3> <p>返回一个 <code>boolean</code> 验证传入值是否是一个 Dayjs 对象.</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>dayjs<span class="token punctuation">.</span><span class="token function">isDayjs</span><span class="token punctuation">(</span><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
dayjs<span class="token punctuation">.</span><span class="token function">isDayjs</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>也可以使用 <code>instanceof</code></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">dayjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">instanceof</span> <span class="token class-name">dayjs</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/my/ui/components/corner-mark.html" class="prev">
        CornerMark 角标
      </a></span> <span class="next"><a href="/my/ui/components/empty.html">
        Empty 空状态
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/my/assets/js/app.2dbd0b81.js" defer></script><script src="/my/assets/js/493.009afff8.js" defer></script><script src="/my/assets/js/1388.8e4e54ce.js" defer></script><script src="/my/assets/js/1246.59f69ef0.js" defer></script><script src="/my/assets/js/1031.8d6cd2c9.js" defer></script>
  </body>
</html>
